package com.xwj.TimeRequest;

import com.xwj.data.StaticData;
import com.xwj.entity.RaOrder;
import com.xwj.service.Impl.RaOrderServiceImpl;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class RaOrderTimeQuestImpl implements TimeRequestTask {

    @Autowired
    private RaOrderServiceImpl raOrderService;

    @Override
    @Scheduled(cron = "0 0/1 * * * ?")
    public void preCheck() {
        List<RaOrder> orderList = raOrderService.findOrderByStatus(StaticData.ORDER_STATUS_NO_CONFIRM);
        List<String> ids = new ArrayList<>();
        orderList.forEach(raOrder -> { //未正式确认的订单在规定内状态还是未确定则订单无效删除
            long expireTime = DateTime.parse(raOrder.getCreateTime(), DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss"))
                    .plusDays(StaticData.ORDER_EFFECT_DAYS).getMillis();
            if (System.currentTimeMillis() >= expireTime)
                ids.add(raOrder.getId());
        });
        if (ids.size() > 0)
            raOrderService.deleteByIds(ids);
    }
}
